//
//  LSLoginViewModel.m
//  RAC实现登录小demo
//
//  Created by 梁森 on 2018/3/26.
//  Copyright © 2018年 梁森. All rights reserved.
//

#import "LSLoginViewModel.h"

@implementation LSLoginViewModel

- (instancetype)init{
    self = [super init];
    if (self) {
        [self setUp];
    }
    return self;
}

- (void)setUp{
    // 登录按钮是否可点击
    self.loginEnable = [RACSignal combineLatest:@[RACObserve(self, self.username), RACObserve(self, self.password)] reduce:^id _Nonnull{
        return @(self.username.length && self.password.length);
    }];
    
    // 按钮的点击事件
    self.loginCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal * _Nonnull(id  _Nullable input) {
        NSLog(@"进行登录操作。。。");
        return [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
            
            [subscriber sendNext:@"finished"];
            /****************** -------- 发送完成这一步很重要, 不然后面的无法信号无法执行 -------- ******************/
            [subscriber sendCompleted];
            return nil;
        }];

    }];
    
    [self.loginCommand.executionSignals.switchToLatest subscribeNext:^(NSDictionary *x) {
        
        NSLog(@"登录返回数据：%@", x);
        
    }];
    
    [[self.loginCommand.executing skip:1] subscribeNext:^(NSNumber * _Nullable x) {
        NSLog(@"登录中...");
        NSLog(@"--%d", [x intValue]);
    }];
}

@end
